package com.itts.middle.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.itts.common.util.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/orderIn")
public class CarController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;
    public String requestGetToken() {
        Map<String, Object> map = new HashMap<>();
        map.put("account", "nftest");
        map.put("password", "123456");
        String url = "http://221.6.109.142:8088" + "/StandardApiAction_login.action";
        String result = HttpRequest.get(url).form(map).timeout(5000).execute().body();

        JSONObject jsonObject = JSONObject.parseObject(result);
        String token = (String) jsonObject.get("jsession");

        log.info("获取车辆信息登录token : {},获取时间 : {}", token, LocalDateTime.now());

        // 存入redis
        redisTemplate.opsForValue().set("DAYUN_CAR_OBTAIN_TOKEN", token, 8L, TimeUnit.MINUTES);
        return token;
    }

    @GetMapping("/getItem")
    public R getitem(){
        Object token = redisTemplate.opsForValue().get("DAYUN_CAR_OBTAIN_TOKEN");
        String url = "http://221.6.109.142:8088" + "/StandardApiAction_queryUserVehicle.action";

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("jsession", token);
        hashMap.put("language", "zh");
        String data = HttpRequest.get(url).form(hashMap).execute().body();
        return success(data);
    }
    @GetMapping("/get")
    public R get(){
        redisTemplate.opsForHash().put("sd","ds",45);
        return R.ok();
    }
}
